﻿#region Copyright(C) 2018 FI Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2018 FI Ltd. All rights reserved.
//
// SYSTEM NAME	    : FI.Net Application Framework
// COMPONENT ID	    : FI.Core
// COMPONENT DESC   : FI.Net Core Foundation
//
// CREATED DATE/BY  : 18 Aug 2018 / Tian Mi
//
// REVISION HISTORY :	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion

using FI.Core.IoC;
using FI.Core.Utilities;
using System;

namespace FI.Core.Caching
{
    /// <summary>
    /// This class is used to register or get cache store factory.
    /// </summary>
    public static class CacheStoreFactories
    {
        #region Fields
        private static ICacheStoreFactory cacheStoreFactory;
        private static Type cacheStoreFactoryType;
        #endregion

        #region Public Methods
        /// <summary>
        /// Sets the cache store factory.
        /// </summary>
        /// <param name="cacheStoreFactory">The cache store factory.</param>
        public static void SetCacheStoreFactory(ICacheStoreFactory cacheStoreFactory)
        {
            Guard.ArgumentNotNull(cacheStoreFactory, "cacheStoreFactory");
            CacheStoreFactories.cacheStoreFactory = cacheStoreFactory;
        }

        /// <summary>
        /// Sets the type of the cache store factory.
        /// </summary>
        /// <param name="cacheStoreFactoryType">Type of the cache store factory.</param>
        public static void SetCacheStoreFactoryType(Type cacheStoreFactoryType)
        {
            Guard.ArgumentNotNull(cacheStoreFactoryType, "cacheStoreFactoryType");
            Guard.TypeIsAssignable(typeof(ICacheStoreFactory), cacheStoreFactoryType, "cacheStoreFactoryType");
            CacheStoreFactories.cacheStoreFactoryType = cacheStoreFactoryType;
        }

        /// <summary>
        /// Gets the cache store factory.
        /// </summary>
        /// <param name="serviceLocatorName">Name of the service locator.</param>
        /// <returns>The service store factory.</returns>
        public static ICacheStoreFactory GetCacheStoreFactory(string serviceLocatorName = null)
        {
            if (null != cacheStoreFactory)
            {
                return cacheStoreFactory;
            }
            if (null != cacheStoreFactoryType)
            {
                return cacheStoreFactory = (ICacheStoreFactory)Activator.CreateInstance(cacheStoreFactoryType);
            }
            return ServiceLocatorFactory.GetServiceLocator(serviceLocatorName).GetService<ICacheStoreFactory>();
        }
        #endregion
    }
}
